package com.ruoyi.wx.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.wx.domain.PtClassExp;
import com.ruoyi.wx.domain.PtExp;
import com.ruoyi.wx.domain.dto.IndexPtExpDTO;
import com.ruoyi.wx.mapper.PtClassExpMapper;
import com.ruoyi.wx.mapper.PtExpMapper;
import com.ruoyi.wx.service.IPtExpService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author moxiu
 * @since 2021-11-13
 */
@Service
public class PtExpServiceImpl extends ServiceImpl<PtExpMapper, PtExp> implements IPtExpService {

	@Autowired
	private PtClassExpMapper ptClassExpMapper;

	@Autowired
	private SysUserMapper sysUserMapper;

	@Override
	public List<SysDept> selectClassList() {
		return baseMapper.selectClassList();
	}

	@Override
	public List<IndexPtExpDTO> allExp() {
		final Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
		final Long deptId = sysUserMapper.selectUserDeptIdByUserId(userId);
		if (deptId == null) {
			return null;
		}
		List<IndexPtExpDTO> list = new ArrayList<>();
		List<PtClassExp> ptClassExps = ptClassExpMapper.selectList(Wrappers.lambdaQuery(PtClassExp.class)
			.eq(PtClassExp::getClassId, deptId)
			.select(PtClassExp::getExpId)
		);
		//遍历本学期需要做的实验集合
		ptClassExps.forEach(e -> {
			IndexPtExpDTO indexPtExpDTO = baseMapper.selectIndePtExpList(e.getExpId(), userId);
			if (indexPtExpDTO == null) {
				IndexPtExpDTO temp = new IndexPtExpDTO();
				temp.setExpId(e.getExpId());
				PtExp ptExp = baseMapper.selectOne(Wrappers.lambdaQuery(PtExp.class)
					.eq(PtExp::getExpId, e.getExpId())
					.select(PtExp::getContent, PtExp::getName)
				);
				temp.setContent(ptExp.getContent());
				temp.setName(ptExp.getName());
				list.add(temp);
			} else {
				list.add(indexPtExpDTO);
			}
		});
		return list;
	}
}
